package com.situ.wms.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.wms.entity.Goods;
import com.situ.wms.entity.GoodsNotes;
import com.situ.wms.impl.GoodsNotesServiceImpl;
import com.situ.wms.impl.ManagerServiceImpl;
import com.situ.wms.service.GoodsNotesService;
import com.situ.wms.service.ManagerService;
import com.situ.wms.util.BaseController;
import com.situ.wms.vo.LayuiTable;
import com.situ.wms.vo.ResultSumVO;



@WebServlet("/goods")
public class GoodsController extends  BaseController{
	private static final long serialVersionUID = 1L;

	ManagerService service =new ManagerServiceImpl();
	GoodsNotesService service2=new GoodsNotesServiceImpl();
	String actionpart2 =null;
	String chargeman2=null;
 
	public void page( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
	}
	public LayuiTable list(Integer page,Integer limit,String keyword) {	
				List<Goods>list=service.select(page,limit,keyword);
				//造一个前台解析格式的vo对象
			
				LayuiTable vo =new LayuiTable();
				//set值
				vo.setCode(0);
				vo.setData(list);
	 			vo.setCount(service.count(keyword));//统计具体几条返回
	 			return vo; 
	}
	//根据id删除一个员工
	public void delete(Integer id) {
		Goods goods =new Goods();
		GoodsNotes goodsnotes =new GoodsNotes();
		goods=service.selectByid(id);
		Date date=new Date(System.currentTimeMillis());
		goodsnotes.setGoodsname(goods.getName());
		goodsnotes.setGoodsfactory(goods.getFactory());
		goodsnotes.setGoodsmodel(goods.getModel());
		goodsnotes.setGoodssize(goods.getSize());
		goodsnotes.setGoodsnumber(goods.getNumber());
		goodsnotes.setActiondate(date);
		goodsnotes.setActionpart(actionpart2);
		goodsnotes.setChargeman(chargeman2);
		goodsnotes.setStatus("出库");
		System.out.println(goodsnotes);
		service2.notesIn(goodsnotes);
		service.delete(id);
	}
	
	public void save(Goods goods) {
		GoodsNotes goodsnotes= new GoodsNotes();
		if(goods.getId()==null) {
			
			//SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
			Date date=new Date(System.currentTimeMillis());
			goodsnotes.setGoodsname(goods.getName());
			goodsnotes.setGoodsfactory(goods.getFactory());
			goodsnotes.setGoodsmodel(goods.getModel());
			goodsnotes.setGoodssize(goods.getSize());
			goodsnotes.setGoodsnumber(goods.getNumber());
			goodsnotes.setActiondate(date);
			goodsnotes.setActionpart(goods.getActionpart());
			goodsnotes.setChargeman(goods.getChargeman());
			goodsnotes.setStatus("入库");
			service.add(goods);
			service2.notesIn(goodsnotes);
		}else {
		
			service.update(goods);
			service2.update(goodsnotes);
		}
	}
	public void save3(GoodsNotes goodsnotes) {
		
	
			
			//SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
			Date date=new Date(System.currentTimeMillis());
			goodsnotes.setId(goodsnotes.getId());
			goodsnotes.setGoodsname(goodsnotes.getGoodsname());
			goodsnotes.setGoodsfactory(goodsnotes.getGoodsfactory());
			goodsnotes.setGoodsmodel(goodsnotes.getGoodsmodel());
			goodsnotes.setGoodssize(goodsnotes.getGoodssize());
			goodsnotes.setGoodsnumber(goodsnotes.getGoodsnumber());
			goodsnotes.setActiondate(date);
			goodsnotes.setActionpart(goodsnotes.getActionpart());
			goodsnotes.setChargeman(goodsnotes.getChargeman());
			goodsnotes.setStatus(goodsnotes.getStatus());

			service2.update(goodsnotes);
		
	}
	public void save2(Goods goods) {
		
		actionpart2=goods.getActionpart();
		chargeman2=goods.getChargeman();
	
	}
	public ResultSumVO insumbyyear(String year) {
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.insumbyyear(year));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}
	public ResultSumVO insumbymonth(String month) {
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.insumbymonth(month));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}public ResultSumVO insumbyday(String day) {
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.insumbyday(day));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}
	public ResultSumVO outsumbyyear(String year) {
		System.out.println(666);
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.outsumbyyear(year));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}
	public ResultSumVO outsumbymonth(String month) {
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.outsumbymonth(month));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}public ResultSumVO outsumbyday(String day) {
		ResultSumVO vo= new ResultSumVO();
		vo.setData(service.outsumbyday(day));
		vo.setCode(0);
		vo.setCount(1L);
		return vo;
	}

	}
	



